/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.groupes;

import gestionedt.models.Groupe;
import gestionedt.models.Module;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * 
 */
public class GroupesForm extends org.apache.struts.action.ActionForm {
    
    private List<Groupe> groupes;

    private int idGroupe;

    private Groupe groupe;

    private String[] numsIndispo;

    private List<Module> modules;

    private String[] idModuleSel;

    private int idModule;

    /**
     *
     */
    public GroupesForm() {
        super();
        this.groupe = new Groupe();
        this.numsIndispo = new String[0];
    }

    public Groupe getGroupe() {
        return groupe;
    }

    public void setGroupe(Groupe groupe) {
        this.groupe = groupe;
    }

    public List<Groupe> getGroupes() {
        return groupes;
    }

    public void setGroupes(List<Groupe> groupes) {
        this.groupes = groupes;
    }

    public int getIdGroupe() {
        return idGroupe;
    }

    public void setIdGroupe(int idGroupe) {
        this.idGroupe = idGroupe;
    }

    public String[] getNumsIndispo() {
        return numsIndispo;
    }

    public int[] getNumsIntIndispo() {
        int[] ids = new int[numsIndispo.length];
        int i = 0;
        for(String id : numsIndispo)
            ids[i++] = Integer.parseInt(id);
        return ids;
    }

    public void setNumsIndispo(String[] numsIndispo) {
        this.numsIndispo = numsIndispo;
    }

    public List<Module> getModules() {
        return modules;
    }

    public void setModules(List<Module> modules) {
        this.modules = modules;
    }

    public String[] getIdModuleSel() {
        return idModuleSel;
    }

    public int getIdIntModuleSel() {
        if(idModuleSel.length > 0) {
            try {
                return Integer.parseInt(idModuleSel[0]);
            } catch(NumberFormatException e) {}
        }
        return 0;
    }

    public void setIdModuleSel(String[] idModuleSel) {
        this.idModuleSel = idModuleSel;
    }

    public int getIdModule() {
        return idModule;
    }

    public void setIdModule(int idModule) {
        this.idModule = idModule;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if(groupe.getLibelle() == null || groupe.getLibelle().trim().length() < 1)
            errors.add("groupe.libelle", new ActionMessage("errors.required",
                                                           "Le champ libellé"));
        return errors;
    }

    public ActionMessages validateInscripGroupe(ActionMapping mapping, HttpServletRequest request) {
        ActionMessages messages = new ActionMessages();
        if(idModuleSel == null
                || idModuleSel.length < 1
                || idModuleSel[0]==null
                || idModuleSel[0].trim().length() < 1)
            messages.add("idModuleSel", new ActionMessage("errors.required", "Le champ module"));

        return messages;
    }

}
